//
//  THSpeechController.m
//  AVFoundationDemo
//
//  Created by rubick on 2019/1/17.
//  Copyright © 2019 LG. All rights reserved.
//

#import "THSpeechController.h"

@interface THSpeechController()<AVSpeechSynthesizerDelegate>

@property (strong, nonatomic, readwrite) AVSpeechSynthesizer *synthesizer;
@property (strong, nonatomic) NSArray *voices;
@property (strong, nonatomic) NSArray *speechStrings;
@property (nonatomic) NSUInteger currentSpeechIndex;

@end

@implementation THSpeechController

+ (instancetype)speechController {
    return [[self alloc] init];
}
- (id)init {
    self = [super init];
    if(self){
        _synthesizer = [[AVSpeechSynthesizer alloc] init];
        _synthesizer.delegate = self;
        _voices = @[[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]];
        _currentSpeechIndex = 0;
        _speechStrings = [self buildSpeechStrings];
    }
    return self;
}
- (NSArray *)buildSpeechStrings {
    return @[
             @"Hello AV Foundation. How are you?",
             @"I'm well! Thanks for asking.",
             @"Are you excited about the book?",
             @"Very! I have always felt si misunderstood",
             @"What's your favorite feature?",
             @"oh, they're all my babies. I counldn't possibly choose.",
             @"It was great to speak with you!",
             @"The pleasure was all mine! Have fun!",
             @"天地玄黄,宇宙洪荒。I'm rubick!"];
}
- (void)beginConversation {
    [self playSpeechOfIndex:0];
}
- (void)playSpeechOfIndex:(NSUInteger)index {
    if(index < self.speechStrings.count) {
        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.speechStrings[index]];
//        utterance.voice = self.voices[i % 2];
        utterance.rate = 0.4f;
        utterance.pitchMultiplier = 0.8f;
        utterance.postUtteranceDelay = 0.1f;
        [self.synthesizer speakUtterance:utterance];
    } else {
        self.currentSpeechIndex = 0;
    }
}
#pragma mark - SpeechSynthesizerDelegate
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"%s",__func__);
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"%s",__func__);
    self.currentSpeechIndex += 1;
    [self playSpeechOfIndex:self.currentSpeechIndex];
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"%s",__func__);
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"%s",__func__);
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
    NSLog(@"%s",__func__);
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
    NSLog(@"%s",__func__);
}

@end
